﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using log4net;

namespace ClimateControl.Infrastructure.Service
{
    public class ClimateManager : ClientBase<IClimateManager>, IClimateManager
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ClimateManager));

        #region Constructor

        public ClimateManager(EventHandler exceptionHandler, string location)
            : base(ServiceHelper.GetBinding(), ServiceHelper.GetEndpointAddress(location))
        {
            this.ChannelFactory.Faulted += exceptionHandler;
            this.InnerChannel.Faulted += exceptionHandler;
        }

        #endregion Constructor

        #region Service Methods

        public void SetIsAirConOn(bool isAirConOn)
        {
            try
            {
                Channel.SetIsAirConOn(isAirConOn);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }

        public void SetTemperature(int temperature)
        {
            try
            {
                Channel.SetTemperature(temperature);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }

        public void SetVoteFrequency(double voteFrequency)
        {
            try
            {
                Channel.SetVoteFrequency(voteFrequency);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }

        public void SetVoteExpiry(int voteExpiry)
        {
            try
            {
                Channel.SetVoteExpiry(voteExpiry);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }

        public void SetIsBigFanOn(bool isBigFanOn)
        {
            try
            {
                Channel.SetIsBigFanOn(isBigFanOn);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }

        public void SetUpdateFrequency(int updateFrequency)
        {
            try
            {
                Channel.SetUpdateFrequency(updateFrequency);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }

        public void ResetVotes()
        {
            try
            {
                Channel.ResetVotes();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }

        public ClimateInfo GetClimateInfo()
        {
            return this.Channel.GetClimateInfo();
        }

        #endregion Service Methods
    }
}
